﻿Imports System.IO
Public Class CarritoFrm
    Dim j As Integer

    Dim productos As New List(Of Producto)
    Private Sub CarritoFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ProductoListBox.DataSource = PRODUCTOTableAdapter.GetData()
        ProductoListBox.DisplayMember = "Nombre"
        Me.PRODUCTOTableAdapter.Fill(Me.ScrumDataSet.PRODUCTO)
    End Sub
    Private Sub btnVolver_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVolver.Click
        Me.Close()
    End Sub

    Private Sub BtnAñadir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAñadir.Click
        If IsNothing(ProductoListBox.SelectedItem) Then Exit Sub
        'Me.milistica.Add(ProductoListBox.SelectedItem())
        ''no añade lo que quiero
        'ListBox1.DataSource = milistica
        Dim plb As DataRowView = ProductoListBox.SelectedItem

        Dim pr As New Producto(plb("nombre"), plb("precio"), plb("descripción"))
        productos.Add(pr)
        ListBox1.DataSource = Nothing
        ListBox1.DataSource = Me.productos
        ListBox1.DisplayMember = "Nombre"
    End Sub

    Private Sub Btnterminado_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnterminado.Click

        Dim ficheroindice As New StreamReader("indicej.txt")
        j = ficheroindice.ReadLine
        ficheroindice.Close()
        Dim rutaFichero As String

        'Dim rutaFichero2 As String
        'rutaFichero2 = Path.Combine(Application.StartupPath, "indicej.txt")
        'Dim fichero2 As New StreamReader(rutaFichero2, True)
        'fichero2.ReadLine()
        'fichero2.Close()

        rutaFichero = Path.Combine(Application.StartupPath, "carrito.txt")
        Dim fichero As New StreamWriter(rutaFichero, True)
        j += 1
        fichero.WriteLine("PEDIDO " & j & ": ")
        For i = 0 To ListBox1.Items.Count - 1

            fichero.WriteLine(ListBox1.Items(i))
        Next
        fichero.Close()

        Dim rutaFichero3 As String
        rutaFichero3 = Path.Combine(Application.StartupPath, "indicej.txt")
        Dim fichero3 As New StreamWriter("indicej.txt", False)

        fichero3.WriteLine(j)
        fichero3.Close()
        'Me.IsMdiContainer = True 'El formulario principal puede contener otros formularios
        Dim canuteiro As New frm_ticket
        canuteiro.dgvTiquet.DataSource = productos
        Dim total As Decimal
        For i = 0 To productos.Count - 1
            total += productos(i).precio
        Next
        canuteiro.dgvTiquet.Columns().Add("Total", "total")

        canuteiro.ShowDialog()
        'frm_ticket.Show()
    End Sub

    Private Sub BtnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEliminar.Click
        If IsNothing(ListBox1.SelectedItem) Then
            MessageBox.Show("SEDS")
            Exit Sub
        End If

        Dim plb As Producto = ListBox1.SelectedItem

        productos.Remove(plb)
        ListBox1.DataSource = Nothing
        ListBox1.DataSource = Me.productos
        ListBox1.DisplayMember = "Nombre"
    End Sub

    Private Sub borrar_todo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles borrar_todo.Click

        productos.Clear()

        ListBox1.DataSource = Nothing
        ListBox1.DataSource = Me.productos
        ListBox1.DisplayMember = "Nombre"
    End Sub

    'Private Sub ProductoListBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ProductoListBox.DragDrop
    '    Dim plb As DataRowView = ProductoListBox.SelectedItem
    '    DoDragDrop(plb, DragDropEffects.Copy)


    '    Dim pr As New Producto(plb("nombre"), plb("precio"), plb("descripción"))
    '    productos.Add(pr)
    '    ListBox1.DataSource = Nothing
    '    ListBox1.DataSource = Me.productos
    '    ListBox1.DisplayMember = "Nombre"
    'End Sub



End Class